﻿using MediatR;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Domain.warehouse;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.Inventory
{
    public class GetMaterialManagementHandler : IRequestHandler<GetMaterialManagementCommand, Result>
    {
        /// <summary>
        /// 仓储仓储
        /// </summary>
        private readonly IBaseRepository<MaterialManagement> materialManagementRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<GetWarehouseHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="warehouseRepository"></param>
        /// <param name="logger"></param>
        public GetMaterialManagementHandler(IBaseRepository<MaterialManagement> materialManagementRepository, ILogger<GetWarehouseHandler> logger)
        {
            this.materialManagementRepository = materialManagementRepository;
            this.logger = logger;
        }
    /// <summary>
        /// 根据仓库id获取物流信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(GetMaterialManagementCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var list = await(await materialManagementRepository.GetAll()).ToListAsync();
                result.Data = list;
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "根据根据仓库id获取物流信息成功";
            }
            catch (Exception ex)
            {
                result.Msg = "根据根据仓库id获取物流信息异常" + ex.Message;
                logger.LogError("根据根据仓库id获取物流信息异常" + ex.Message);
            }
            return result;
        }
    }
}
